<template>
   <div  style="height:100%;" class="login-wrap">
        <div style="height:11%;color: #444444;font-size: 36px;" >
            <div style="height:100%;color: #444444;font-size: 36px;width:1080px;margin:0 auto;font-weight:normal" class="flex flex-align-center">
               欢迎登录
            </div>
        </div>
        <div class="inv-login">
           <div class="logo-box position-middle-center">
               <div class="left-logo left">
                   <div class="include flex flex-align-center flex-pack-center">
                       <p>火星捕手-库存管理系统</p>
                   </div>
               </div>
               <form @submit.prevent="login" class="right-user left" v-if="!(employeeStore&&employeeStore[1])">
                   <p class="title">登录</p>
                   <div class="phone login-input-default" >
                       <input type="tel" placeholder="请输入账号" v-model="phone" autocomplete="off">
                       <i class="iconfont icon-mobile"></i>
                   </div>
                   <div class="password login-input-default">
                       <input type="password" placeholder="请输入密码" v-model="password" autocomplete="off">
                       <i class="iconfont icon-mima"></i>
                   </div>
                   <p style="font-size:14px;margin-bottom:30px;text-align:center;">
                    酒吧/KTV/茶楼等库存登录？<a href="https://table-inventory.huoxingy.com/#/login">点击切换至旗舰版</a>
                   </p>
                   <div class="btn" style="margin-top:10px">
                       <button type="submit">登录</button>
                   </div>
                   <div class="forget">
                       忘记密码?<span @click="$router.push({path:'./resetPassword'})">点击重置</span>
                   </div>
               </form>
               <div class="right-user choice left" v-if="employeeStore&&employeeStore[1]">
                   <div class="title">请选择店铺</div>
                   <div class="closeBtn" @click="employeeStore=[]">&times;</div>
                   <div class="scroll-box">
                       <div class="content">
                           <div class="store-item"  v-for="item in employeeStore"  @click="choiceStore(item)">
                               <span v-text="item.storeName"></span>
                               <span v-text="item.employeeName"></span>
                           </div>
                       </div>
                   </div>
               </div>
           </div>
        </div>
        <div class="com-info">
            <p><a target="_blank" href="http://www.beian.miit.gov.cn">蜀ICP备17028712号-1</a> 火星云网络科技有限公司 huoxingy.com</p>
        </div>
   </div>
</template>

<script type="text/javascript">
    export default{
        data () {
            return {
                checked: true,
                phone: '', // 手机号
                password: '' ,// 密码
                employeeStore: [],
            }
        },
        methods: {
            choiceStore(item){
                sessionStorage.setItem('store', JSON.stringify(item))
                this.$http.post('authorize?storeId='+item.storeId, {}).then((res)=>{//授权
                    this.$router.push({path:'/Home'});
                })
            },
            login: function () { // 登录事件
                if ( !(/^1[3|4|5|6|7|8|9][0-9]{9}$/.test(this.phone)) ) {
                    this.$message.error({
                        showClose: true,
                        message: "请输入正确的手机号",
                        type: 'error'
                    });
                    return;
                }
                if ( !this.password ) {
                    this.$message.error({
                        showClose: true,
                        message: "请输入密码",
                        type: 'error'
                    });
                    return;
                }
                let data = {
                    "username": this.phone,
                    // "password": hex_md5(this.password),
                    "password": this.password,
                }
                let b = new Base64();
                let authStr = 'Basic '+ b.encode(data.username+':'+data.password);

                this.$http.post('/stock/login', {}, {
                    auth: data // 添加登录验证
                }).then((res)=>{
                    this.employeeStore = res.data;
                    sessionStorage.setItem('token', authStr);
                    sessionStorage.setItem('store', JSON.stringify(res.data));
                    if(!res.data[0]){
                        this.$message({
                            showClose: true,
                            message: "暂无登录权限",
                            type: 'error'
                        })
                        return; 
                    }
                    sessionStorage.setItem('allStore', JSON.stringify(res.data));
                    //一个店铺直接去库存
                    if(!res.data[1]){
                        this.choiceStore(res.data[0]);
                    }
                }).catch((err)=>{
                    if ( err.response ) {
                        if ( err.response.status == 401 ) {
                            this.$message({
                                showClose: true,
                                message: "帐号或者密码有误",
                                type: 'error'
                            })
                        } else if ( err.response.status == 403 ) {
                            this.$message({
                                showClose: true,
                                message: "权限不足",
                                type: 'error'
                            })
                        } else {
                            this.$message({
                                showClose: true,
                                message: err.response.data,
                                type: 'error'
                            })
                        }
                    }
                })
            }
        },
        created(){
           
        }
    }
</script>
<style lang="scss" scoped>
    @import '../../../static/css/common.scss';
    @import "login";
</style>
